//package com.qunchuang.timers;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.qunchuang.config.LiftIpConfig;
//import com.qunchuang.constant.LiftConstant;
//import com.qunchuang.constant.LiftStorageLocationConstant;
//import com.qunchuang.constant.LiftTaskStateConstant;
//import com.qunchuang.mapper.LiftAgvcHistoryMapper;
//import com.qunchuang.mapper.LiftAgvcTaskMapper;
//import com.qunchuang.mapper.LiftMapper;
//import com.qunchuang.mapper.LiftStorageLocationMapper;
//import com.qunchuang.pojo.dto.AgvLiftEndFloorDto;
//import com.qunchuang.pojo.dto.AgvLiftStorageLocationDto;
//import com.qunchuang.pojo.entity.LiftAgvcHistoryEntity;
//import com.qunchuang.pojo.entity.LiftAgvcTaskEntity;
//import com.qunchuang.pojo.entity.LiftEntity;
//import com.qunchuang.pojo.entity.LiftStorageLocationEntity;
//import com.qunchuang.pojo.vo.LiftFirefightingVo;
//import com.qunchuang.pojo.vo.ResponseVo;
//import com.qunchuang.utils.Modbus4jUtils;
//import com.serotonin.modbus4j.code.DataType;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RestTemplate;
//
//import java.nio.charset.StandardCharsets;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Component
//public class LiftTaskStateTimers {
//    @Autowired
//    private LiftAgvcTaskMapper liftAgvcTaskMapper;
//    @Autowired
//    private LiftMapper liftMapper;
//    @Autowired
//    private LiftStorageLocationMapper liftStorageLocationMapper;
//    @Autowired
//    private RestTemplate restTemplate;
//    @Autowired
//    private LiftAgvcHistoryMapper liftAgvcHistoryMapper;
//    @Autowired
//    private LiftIpConfig liftIpConfig;
//    public void stateTimers() throws Exception {
//        log.info("配置文件参数：{}",liftIpConfig);
//        String liftConfigIp = liftIpConfig.getIp();
//        Integer liftConfigPort = liftIpConfig.getPort();
//        log.info("状态机");
//        //todo 检查电梯消防模式是否关闭
//        Number liftFirefightingOff = Modbus4jUtils.readHoldingRegister(liftConfigIp, liftConfigPort, 1, 7, DataType.TWO_BYTE_INT_SIGNED);
//        int liftFirefightingOffInt = liftFirefightingOff.intValue();
//        if (liftFirefightingOffInt == 2) {
//            ResponseVo responseVo = new ResponseVo();
//            responseVo.setReturnCode(1);
//            log.info("消防模式关闭");
//            //修改电梯表，消防关闭
//            QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//            QueryWrapper<LiftEntity> liftFirefighting = liftEntityQueryWrapper.eq("lift_firefighting", LiftConstant.LIFT_FIREFIGHTING_OPEN);
//            List<LiftEntity> liftEntities = liftMapper.selectList(liftFirefighting);
//            List<String> liftIdS = liftEntities.stream().map(LiftEntity::getLiftId).collect(Collectors.toList());
//            if (!liftIdS.isEmpty()){
//                QueryWrapper<LiftEntity> entityQueryWrapper = new QueryWrapper<>();
//                LiftEntity lift = new LiftEntity();
//                lift.setLiftFirefighting(LiftConstant.LIFT_FIREFIGHTING_SHUT_DOWN);
//                entityQueryWrapper.in("lift_id",liftIdS);
//                liftMapper.update(lift,entityQueryWrapper);
//            }
//
////            String url = "http://localhost:8080/fire/control/off?ReturnCode=" + responseVo.getReturnCode();
////
////            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
////
////            restTemplate.getForObject(url, String.class);
//        }
//
//        ArrayList<LiftAgvcTaskEntity> liftAgvcTaskList = liftAgvcTaskMapper.selectAllTask();
//        if (liftAgvcTaskList !=null && !liftAgvcTaskList.isEmpty()){
//            //查询任务表中有数据
//            for (LiftAgvcTaskEntity liftAgvcTask : liftAgvcTaskList) {
//                String liftId = liftAgvcTask.getLiftId();
//                Integer liftEndFloor = liftAgvcTask.getLiftEndFloor();
//                Integer liftStartFloor = liftAgvcTask.getLiftStartFloor();
//                String storageLocationId = liftAgvcTask.getStorageLocationId();
//                String agvId = liftAgvcTask.getAgvId();
//                Integer id = liftAgvcTask.getId();
//                Integer booking = liftAgvcTask.getBooking();
//                String requestDot = liftAgvcTask.getRequestDot();
//                Date startTime = liftAgvcTask.getStartTime();
//                //根据电梯查询电梯ip，端口号
//                QueryWrapper<LiftEntity> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("lift_id",liftId);
//                LiftEntity getLiftEntity = liftMapper.selectOne(queryWrapper);
//                String ip = getLiftEntity.getLiftIp();
//                Integer port = getLiftEntity.getLiftPort();
//                AgvLiftStorageLocationDto dto = new AgvLiftStorageLocationDto();
//                dto.setAgvId(agvId);
//                dto.setLiftCode(liftId);
//                dto.setStoragePoint(storageLocationId);
//                AgvLiftEndFloorDto agvLiftEndFloorDto = new AgvLiftEndFloorDto();
//                agvLiftEndFloorDto.setAgvId(agvId);
//                agvLiftEndFloorDto.setLiftCode(liftId);
//                agvLiftEndFloorDto.setArriveFloor(liftEndFloor);
//
//
//
//                ArrayList<LiftFirefightingVo> liftFirefightingList = new ArrayList<>();
//                LiftFirefightingVo firefightingDto = new LiftFirefightingVo();
//                //todo 检查电梯消防模式是否开启
//                Number liftFirefighting = Modbus4jUtils.readHoldingRegister(ip, port, 1, 7, DataType.TWO_BYTE_INT_SIGNED);
//                int liftFirefightingInt = liftFirefighting.intValue();
//                if (liftFirefightingInt == 1) {
//                    log.info("电梯消防模式为1开启");
//                    //电梯消防模式开启，查询所有电梯中的agv
//                    QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
//                    liftAgvcTaskEntityQueryWrapper.ge("state", LiftTaskStateConstant.AGV_ARRIVE_LIFT_STORAGE);
//                    List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvcTaskEntityQueryWrapper);
//                    for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
//                        String liftCode = liftAgvcTaskEntity.getLiftId();
//                        firefightingDto.setLiftCode(liftCode);
//                        //查询储位表获取agvId
//                        QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
//                        liftStorageLocationEntityQueryWrapper.eq("storage_location_id", liftCode);
//                        liftStorageLocationEntityQueryWrapper.eq("storage_state", LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
//                        List<LiftStorageLocationEntity> liftStorageLocationEntities = liftStorageLocationMapper.selectList(liftStorageLocationEntityQueryWrapper);
//                        List<String> agvIdList = liftStorageLocationEntities.stream().map(LiftStorageLocationEntity::getAgvId).collect(Collectors.toList());
//                        firefightingDto.setAgvList(agvIdList);
//                        liftFirefightingList.add(firefightingDto);
//                    }
//
//                    ObjectMapper objectMapper = new ObjectMapper();
//                    String jsonStudents = objectMapper.writeValueAsString(liftFirefightingList);
//                    // 设置请求头
//                    HttpHeaders headers = new HttpHeaders();
//                    headers.setContentType(MediaType.APPLICATION_JSON);
//                    headers.setAcceptCharset(com.sun.tools.javac.util.List.of(StandardCharsets.UTF_8));
//
//                    HttpEntity<String> requestEntity = new HttpEntity<>(jsonStudents, headers);
//                    log.info("消防模式开启");
////            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
////            restTemplate.postForObject("http://localhost:8080/fire/control/on", requestEntity, String.class);
//
//                    //清空任务表，转移到历史任务表中，标记为消防模式
//                    for (LiftAgvcTaskEntity liftAgvTaskEntity : liftAgvcTaskList) {
//                        LiftAgvcHistoryEntity liftAgvcHistoryEntity = new LiftAgvcHistoryEntity();
//                        BeanUtils.copyProperties(liftAgvTaskEntity, liftAgvcHistoryEntity);
//                        liftAgvcHistoryEntity.setState(LiftTaskStateConstant.LIFT_FIREFIGHTING_MODE_OPEN);
//                        liftAgvcHistoryEntity.setEndTime(new Date());
//                        liftAgvcHistoryMapper.insert(liftAgvcHistoryEntity);
//                        //修改储位表
//                        LiftStorageLocationEntity liftStorageLocationEntity = new LiftStorageLocationEntity();
//                        liftStorageLocationEntity.setStorageState(LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_FREE);
//                        liftStorageLocationEntity.setAgvId("");
//                        QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
//                        liftStorageLocationEntityQueryWrapper.eq("storage_location_id", liftAgvTaskEntity.getStorageLocationId());
//                        liftStorageLocationMapper.update(liftStorageLocationEntity, liftStorageLocationEntityQueryWrapper);
//                        //修改电梯表
//                        LiftEntity liftEntity = new LiftEntity();
//                        liftEntity.setLiftFirefighting(LiftConstant.LIFT_FIREFIGHTING_OPEN);
//                        liftEntity.setLiftState(LiftConstant.LIFT_STATE_IDLE);
//                        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//                        liftEntityQueryWrapper.eq("lift_id", liftAgvTaskEntity.getLiftId());
//                        liftMapper.update(liftEntity, liftEntityQueryWrapper);
//                    }
//                    List<Integer> idList = liftAgvcTaskList.stream().map(LiftAgvcTaskEntity::getId).collect(Collectors.toList());
//                    liftAgvcTaskMapper.deleteBatchIds(idList);
//
//                    List<String> liftEntities = liftAgvcTaskList.stream().map(LiftAgvcTaskEntity::getLiftId).distinct().collect(Collectors.toList());
//                    log.info("电梯集合大小：{}", liftEntities.size());
//                    //todo 消防开启后，电梯到达一楼
//                    int liftSize = liftEntities.size();
//                    boolean flag = false;
//                    List<String> entitiesToRemove = new ArrayList<>();
//                    while (!flag) {
//                        for (String liftEntityS : liftEntities) {
//                            Number number = Modbus4jUtils.readHoldingRegister(ip, port, 1, 4, DataType.TWO_BYTE_INT_SIGNED);
//                            int groundFloor = number.intValue();
//                            if (groundFloor == 1) {
//                                LiftEntity liftEntity = new LiftEntity();
//                                liftEntity.setLiftRemainFloor(groundFloor);
//                                QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//                                liftEntityQueryWrapper.eq("lift_id", liftEntityS);
//                                liftMapper.update(liftEntity, liftEntityQueryWrapper);
//                                //记录电梯到达1楼
//                                log.info("消防模式开启后");
////                        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
////                        restTemplate.postForObject("http://localhost:8080/sayhello1/post", liftCode, String.class);
//                                entitiesToRemove.add(liftEntityS);
//                                liftSize--;
//                                log.info("size大小为：{}", liftSize);
//                            }
//                        }
//                        // 从原集合中移除需要移除的元素
//                        liftEntities.removeAll(entitiesToRemove);
//                        entitiesToRemove.clear(); // 清空辅助集合
//                        if (liftSize == 0) {
//                            log.info("结束死循环");
//                            flag = true;
//                        }
//                    }
//                }
//
//
//                switch (liftAgvcTask.getState()){
//                    case 0:
//                        //TODO 模拟
//                        //先进行读取电梯信息，是否为空闲状态
//                            Number number = Modbus4jUtils.readHoldingRegister(ip, port, 1, 0, DataType.TWO_BYTE_INT_SIGNED);
//                            int value = number.intValue();
//                            if (value == 1){
//                                QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//                                liftEntityQueryWrapper.eq("lift_id",liftId);
//                                LiftEntity liftEntity = liftMapper.selectOne(liftEntityQueryWrapper);
//                                Integer liftState = liftEntity.getLiftState();
//                                if (liftState == 1){
//                                    QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = setLiftAgvcTaskEntityQueryWrapper(liftId,LiftTaskStateConstant.LIFT_ASSIGNMENT_STORAGE_LOCATION);
//                                    Long liftAgvTaskCount = liftAgvcTaskMapper.selectCount(liftAgvcTaskEntityQueryWrapper);
//                                    if (liftAgvTaskCount == 1){
//                                        QueryWrapper<LiftAgvcTaskEntity> agvcTaskEntityQueryWrapper = setLiftAgvcTaskEntityQueryWrapper(liftId, LiftTaskStateConstant.LIFT_ASSIGNMENT_STORAGE_LOCATION);
//                                        LiftAgvcTaskEntity liftAgvcTaskEntity = liftAgvcTaskMapper.selectOne(agvcTaskEntityQueryWrapper);
//                                        Integer liftStartFloorTask = liftAgvcTaskEntity.getLiftStartFloor();
//                                        Integer liftEndFloorTask = liftAgvcTaskEntity.getLiftEndFloor();
//                                        if (Objects.equals(liftStartFloor, liftStartFloorTask) && Objects.equals(liftEndFloor, liftEndFloorTask)){
//                                            //判断电梯是否是预约字段，预约字段需要修改储位表
//                                            if (booking ==1){
//                                                //创建储位表
//                                                LiftStorageLocationEntity liftStorageLocationEntity = new LiftStorageLocationEntity();
//                                                liftStorageLocationEntity.setStorageState(LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
//                                                liftStorageLocationEntity.setAgvId(agvId);
//                                                QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
//                                                liftStorageLocationEntityQueryWrapper.eq("storage_location_id",storageLocationId);
//                                                liftStorageLocationMapper.update(liftStorageLocationEntity,liftStorageLocationEntityQueryWrapper);
//                                            }
//                                            //修改任务表状态为1,预约状态为0
//                                            updateLiftTaskState(id,LiftTaskStateConstant.CALL_LIFT);
//                                        }
//                                    }
//                                }
//                            } else if (value == 0){
//                                log.info("当前电梯状态为：{}",value);
//                                boolean b1 = Modbus4jUtils.writeRegister(ip, port, 1, 0, (short) 1);
//                                byte startFloor = liftStartFloor.byteValue();
//                                byte endFloor = liftEndFloor.byteValue();
//                                boolean b = Modbus4jUtils.writeRegisters(ip, port, 1, 1, new short[]{0x21,startFloor,endFloor});
//                                if (b){
//                                    //判断电梯是否是预约字段，预约字段需要修改储位表
//                                    if (booking == 1){
//                                        //创建储位表
//                                        LiftStorageLocationEntity liftStorageLocationEntity = new LiftStorageLocationEntity();
//                                        liftStorageLocationEntity.setStorageState(LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
//                                        liftStorageLocationEntity.setAgvId(agvId);
//                                        QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
//                                        liftStorageLocationEntityQueryWrapper.eq("storage_location_id",storageLocationId);
//                                        liftStorageLocationMapper.update(liftStorageLocationEntity,liftStorageLocationEntityQueryWrapper);
//                                    }
//                                    //修改任务表状态为1,预约状态为0
//                                    updateLiftTaskState(id,LiftTaskStateConstant.CALL_LIFT);
//                                }
//                            }
//                        //状态为0进行呼叫电梯
//                        //TODO: （读取电梯是否为空闲）根据任务表的起始楼层向电梯模块发送呼叫电梯指令,直到电梯呼叫成功
//
//                        //判断电梯是否是预约字段，预约字段需要修改储位表
////                        if (booking ==1){
////                            //创建储位表
////                            LiftStorageLocationEntity liftStorageLocationEntity = new LiftStorageLocationEntity();
////                            liftStorageLocationEntity.setStorageState(LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
////                            liftStorageLocationEntity.setAgvId(agvId);
////                            QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
////                            liftStorageLocationEntityQueryWrapper.eq("storage_location_id",storageLocationId);
////                            liftStorageLocationMapper.update(liftStorageLocationEntity,liftStorageLocationEntityQueryWrapper);
////                        }
////                        //修改任务表状态为1,预约状态为0
////                        updateLiftTaskState(id,LiftTaskStateConstant.CALL_LIFT);
//                        break;
//                    case 1:
//                        //TODO 模拟查看电梯当前状态
//
//                            Number number1 = Modbus4jUtils.readHoldingRegister(ip, port, 1, 4, DataType.TWO_BYTE_INT_SIGNED);
//                            //判断电梯是否到达起始楼层
//                            int startFloor = number1.intValue();
//                            if (startFloor == liftStartFloor){
//                                //修改任务表状态为2
//                                updateLiftTaskState(id,LiftTaskStateConstant.LIFT_ARRIVE_CALL_FLOOR);
//                                //修改电梯表，停留楼层字段,起始楼层
////                                updateFloor(liftStartFloor,liftId);
//                                updateLiftTable (liftStartFloor, LiftConstant.LIFT_STATE_RECEIVING_TASK,liftId);
//                            }
//                        //TODO: 状态为1，向电梯模块发送到达起始楼层命令，电梯到达起始楼层
//
//                        //修改任务表状态为2
////                        updateLiftTaskState(id,LiftTaskStateConstant.LIFT_ARRIVE_CALL_FLOOR);
////                        //修改电梯表，停留楼层字段,起始楼层
////                        updateFloor(liftStartFloor,liftId);
//                        break;
//                    case 2:
//                        //查询电梯是否已经开门，开门了不需要再次发送开门命令
//                        //todo 电梯发送开门指令，需要时间等待让他再次发送指令
//                        LiftEntity selectLiftEntity = selectLift(liftId);
//                        Integer liftDoor = selectLiftEntity.getLiftDoor();
//                        //电梯门关闭，需要发送开门指令
//                        if (Objects.equals(liftDoor, LiftConstant.LIFT_DOOR_CLOSE)){
//                            //todo 模拟，发送电梯开门指令，直到电梯门完全打开
//                            boolean b = Modbus4jUtils.writeRegister(ip, port, 1, 5, (short) 3);
//                            if (b){
//                                Number number2 = Modbus4jUtils.readHoldingRegister(ip, port, 1, 6, DataType.TWO_BYTE_INT_SIGNED);
//                                int value1 = number2.intValue();
//                                if (value1 == 1){
//                                    log.info("发送开门指令");
//                                    //修改电梯表，电梯门为1，表示电梯门已完全打开，电梯状态为1
//                                    updateLiftDoor(liftId,LiftConstant.LIFT_DOOR_OPEN);
//                                    //修改任务表状态为3
//                                    updateLiftTaskState(id,LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
//                                }
//                            }
//                            //TODO: 状态为2，向电梯模块发送开门命令，电梯门完全打开，
//
//                        }else {
//                            //修改任务表状态为3
//                            updateLiftTaskState(id,LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
//                        }
//                        //修改电梯表，电梯门为1，表示电梯门已完全打开，电梯状态为1
////                        updateLiftDoorAndState(liftId,LiftConstant.LIFT_DOOR_OPEN,LiftConstant.LIFT_STATE_RECEIVING_TASK);
////                        //修改任务表状态为3
////                        updateLiftTaskState(id,LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
//                        break;
//                    case 3:
//                        //TODO: 状态为3，通知agvc可以进入电梯储位，直到通知成功
//
//                        //通知agvc可以进入电梯
////                        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
////                        restTemplate.postForObject("http://localhost:8080/lift/enter", dto , AgvLiftStorageLocationDto.class);
//                        log.info("通知agv成功");
//                        //状态任务表改为4
//                        updateLiftTaskState(id,LiftTaskStateConstant.NOTIFY_AGV_LIFT_ARRIVED_START_FLOOR);
//                        break;
//                    case 4:
//                        break;
//                    case 5:
//                        //查询电梯是否已经关门，关门了不需要再次发送关门命令
//                        //todo 电梯发送关门指令，需要时间等待让他再次发送指令
//                        Number register = Modbus4jUtils.readHoldingRegister(ip, port, 1, 6, DataType.TWO_BYTE_INT_SIGNED);
//                        int inted = register.intValue();
//                        //电梯门打开状态，需要发送关门命令
//                        if (inted == 1) {
//                            //todo 模拟电梯关门指令
//                            boolean b3 = Modbus4jUtils.writeRegister(ip, port, 1, 5, (short) 4);
//                            if (b3) {
//                                //发送关门命令时，修改电梯表，电梯门为0，表示电梯门已完全关闭，电梯状态为2，处于工作电梯，
//                                updateLiftDoorAndState(liftId, LiftConstant.LIFT_DOOR_CLOSE, LiftConstant.LIFT_STATE_EXECUTING_TASK);
//                                Number number2 = Modbus4jUtils.readHoldingRegister(ip, port, 1, 6, DataType.TWO_BYTE_INT_SIGNED);
//                                int value1 = number2.intValue();
//                                if (value1 == 0) {
//                                    //修改任务表状态为6
//                                    updateLiftTaskState(id, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_CLOSED);
//                                }
//                            }
//                        } else if (inted == 0){
//                            //修改任务表状态为6
//                            updateLiftTaskState(id, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_CLOSED);
//                        }
//
//
//                            //TODO: 状态为5，向电梯模块发送关门命令，电梯门完全关闭，修改任务表状态为6
//                            //发送关门命令时，修改电梯表，电梯门为0，表示电梯门已完全关闭，电梯状态为2，处于工作电梯，
////                            updateLiftDoorAndState(liftId,LiftConstant.LIFT_DOOR_CLOSE,LiftConstant.LIFT_STATE_EXECUTING_TASK);
//                        //修改任务表状态为6
////                        updateLiftTaskState(id,LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_CLOSED);
//                        break;
//                    case 6:
//                        //todo 模拟发送前往最终楼层指令
//                        short i = liftEndFloor.shortValue();
//                        boolean b4 = Modbus4jUtils.writeRegister(ip, port, 1, 6, i);
//                        if (b4){
//                            Number number2 = Modbus4jUtils.readHoldingRegister(ip, port, 1, 4, DataType.TWO_BYTE_INT_SIGNED);
//                            int intValue = number2.intValue();
//                            if (intValue == liftEndFloor){
//                                //修改任务表状态为7
//                                updateLiftTaskState(id,LiftTaskStateConstant.LIFT_ARRIVE_FINAL_FLOOR);
//                                //修改电梯表，停留楼层字段,最终楼层
//                                updateFloor(liftEndFloor,liftId);
//                            }
//                        }
//
//                        //TODO: 状态为6，向电梯模块发送到达最终楼层命令，电梯到达最终楼层
//
//                        //修改任务表状态为7
////                        updateLiftTaskState(id,LiftTaskStateConstant.LIFT_ARRIVE_FINAL_FLOOR);
////                        //修改电梯表，停留楼层字段,最终楼层
////                        updateFloor(liftEndFloor,liftId);
//                        break;
//                    case 7:
//                        //查询电梯是否已经开门，开门了不需要再次发送开门命令
//                        LiftEntity liftEntity = selectLift(liftId);
//                        Integer liftEntityLiftDoor = liftEntity.getLiftDoor();
//                        if (Objects.equals(liftEntityLiftDoor, LiftConstant.LIFT_DOOR_CLOSE)) {
//                            //todo 模拟发送开门命令
//                            boolean b1 = Modbus4jUtils.writeRegister(ip, port, 1, 5, (short) 3);
//                            if (b1) {
//                                Number number2 = Modbus4jUtils.readHoldingRegister(ip, port, 1, 6, DataType.TWO_BYTE_INT_SIGNED);
//                                int value1 = number2.intValue();
//                                if (value1 == 1) {
//                                    //修改电梯表，电梯门为1，表示电梯门已完全打开
//                                    updateLiftDoor(liftId, LiftConstant.LIFT_DOOR_OPEN);
//                                    //修改任务表状态为8
//                                    updateLiftTaskState(id, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN_NOTIFY_AGV);
//                                }
//                            }
//                        } else {
//                            //修改任务表状态为8
//                            updateLiftTaskState(id, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN_NOTIFY_AGV);
//                        }
//                        //电梯门关闭，需要发送开门指令
//                        //TODO: 状态为7，向电梯模块发送开门命令，电梯门完全打开，
//                        //修改电梯表，电梯门为1，表示电梯门已完全打开
////                        updateLiftDoor(liftId,LiftConstant.LIFT_DOOR_OPEN);
////                        //修改任务表状态为8
////                        updateLiftTaskState(id,LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN_NOTIFY_AGV);
//                        break;
//                    case 8:
//                        //TODO： 状态为8，通知agvc，可以离开电梯储位，直到通知成功
//
//                        //电梯空闲，直接呼叫电梯
////                        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
////                        restTemplate.postForObject("http://localhost:8080/lift/enter", agvLiftEndFloorDto , AgvLiftEndFloorDto.class);
//
//                        //任务表状态改为9
//                        updateLiftTaskState(id,LiftTaskStateConstant.NOTIFY_AGVC_LIFT_ARRIVED_FINAL_FLOOR);
//                        break;
//                    case 9:
//                        break;
//                    case 10:
//                        QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
//                        liftStorageLocationEntityQueryWrapper.eq("lift_id", liftId);
//                        liftStorageLocationEntityQueryWrapper.eq("storage_state",LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
//                        Long liftStorageLocationEntityCount = liftStorageLocationMapper.selectCount(liftStorageLocationEntityQueryWrapper);
//                        if (liftStorageLocationEntityCount == 0){
//                            //todo 模拟发送关门命令，
//                            LiftEntity selectLiftTable = selectLift(liftId);
//                            Integer liftDoorStart = selectLiftTable.getLiftDoor();
//                            //电梯门打开状态，需要发送关门命令
//                            if (Objects.equals(liftDoorStart, LiftConstant.LIFT_DOOR_OPEN)) {
//                                boolean b2 = Modbus4jUtils.writeRegister(ip, port, 1, 5, (short) 4);
//                                if (b2) {
//                                    log.info("状态为10，发送关门指令");
//                                    Number number2 = Modbus4jUtils.readHoldingRegister(ip, port, 1, 6, DataType.TWO_BYTE_INT_SIGNED);
//                                    int value1 = number2.intValue();
//                                    if (value1 == 0) {
//                                        //修改电梯表，电梯门状态为0
//                                        updateLiftDoor(liftId, LiftConstant.LIFT_DOOR_CLOSE);
//                                        //修改任务表状态为11
//                                        updateLiftTaskState(id, LiftTaskStateConstant.LIFT_CLOSE_DOOR_RELEASE_TASK);
//                                    }
//                                }
//                            } else {
//                                //修改任务表状态为11
//                                updateLiftTaskState(id, LiftTaskStateConstant.LIFT_CLOSE_DOOR_RELEASE_TASK);
//                            }
//                        }
//                            //TODO: 状态为10，向电梯模块发送关门命令，电梯门完全关闭，
//                        //修改电梯表，电梯门状态为0
////                        updateLiftDoor(liftId,LiftConstant.LIFT_DOOR_CLOSE);
////                        //修改任务表状态为11
////                        updateLiftTaskState(id,LiftTaskStateConstant.LIFT_CLOSE_DOOR_RELEASE_TASK);
//                        break;
//                    case 11:
//                        //释放任务，电梯
//                        //电梯表
//                        LiftEntity liftEntity1 = new LiftEntity();
//                        liftEntity1.setLiftRemainFloor(liftEndFloor);
//                        liftEntity1.setLiftState(LiftConstant.LIFT_STATE_IDLE);
//                        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//                        liftEntityQueryWrapper.eq("lift_id",liftId);
//                        liftMapper.update(liftEntity1,liftEntityQueryWrapper);
//                        //任务表移到历史任务表
//                        LiftAgvcHistoryEntity liftAgvcHistoryEntity = new LiftAgvcHistoryEntity();
////                        liftAgvcHistoryEntity.setId(id);
////                        liftAgvcHistoryEntity.setLiftId(liftId);
////                        liftAgvcHistoryEntity.setAgvId(agvId);
////                        liftAgvcHistoryEntity.setStorageLocationId(storageLocationId);
////                        liftAgvcHistoryEntity.setRequestDot(requestDot);
////                        liftAgvcHistoryEntity.setLiftStartFloor(liftStartFloor);
////                        liftAgvcHistoryEntity.setLiftEndFloor(liftEndFloor);
////                        liftAgvcHistoryEntity.setStartTime(startTime);
////                        liftAgvcHistoryEntity.setEndTime(new Date());
////                        liftAgvcHistoryEntity.setState(LiftTaskStateConstant.LIFT_CLOSE_DOOR_RELEASE_TASK);
////                        liftAgvcHistoryEntity.setBooking(booking);
//                        BeanUtils.copyProperties(liftAgvcTask,liftAgvcHistoryEntity);
//                        liftAgvcHistoryEntity.setEndTime(new Date());
//                        liftAgvcHistoryEntity.setState(LiftTaskStateConstant.LIFT_CLOSE_DOOR_RELEASE_TASK);
//                        liftAgvcHistoryMapper.insert(liftAgvcHistoryEntity);
//                        //删除任务表
//                        liftAgvcTaskMapper.deleteById(id);
//
//                        //todo 模拟电梯，释放电梯处于空闲状态
//                        Modbus4jUtils.writeRegister(ip, port, 1, 0, (short) 0);
//                        break;
//                    default:
//                        break;
//                }
//            }
//        }
//    }
//
//    private void updateLiftTable(Integer liftStartFloor,Integer liftState , String liftId) {
//        LiftEntity lift = new LiftEntity();
//        lift.setLiftRemainFloor(liftStartFloor);
//        lift.setLiftState(liftState);
//        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//        liftEntityQueryWrapper.eq("lift_id", liftId);
//        liftMapper.update(lift,liftEntityQueryWrapper);
//    }
//
//    private static QueryWrapper<LiftAgvcTaskEntity> setLiftAgvcTaskEntityQueryWrapper(String liftId,Integer state) {
//        QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
//        liftAgvcTaskEntityQueryWrapper.eq("lift_id", liftId);
//        liftAgvcTaskEntityQueryWrapper.ne("state",state);
//        return liftAgvcTaskEntityQueryWrapper;
//    }
//
//
//    private void updateFloor(Integer floor,String liftId) {
//        LiftEntity lift = new LiftEntity();
//        lift.setLiftRemainFloor(floor);
//        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//        liftEntityQueryWrapper.eq("lift_id",liftId);
//        liftMapper.update(lift,liftEntityQueryWrapper);
//    }
//
//    private void updateLiftDoor(String liftId,Integer liftDoor) {
//        LiftEntity liftEntity1 = new LiftEntity();
//        liftEntity1.setLiftDoor(liftDoor);
//        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//        liftEntityQueryWrapper.eq("lift_id", liftId);
//        liftMapper.update(liftEntity1,liftEntityQueryWrapper);
//    }
//
//    private void updateLiftDoorAndState(String liftId,Integer liftDoor,Integer liftState) {
//        LiftEntity liftEntity = new LiftEntity();
//        liftEntity.setLiftDoor(liftDoor);
//        liftEntity.setLiftState(liftState);
//        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//        liftEntityQueryWrapper.eq("lift_id", liftId);
//        liftMapper.update(liftEntity,liftEntityQueryWrapper);
//    }
//
//    private LiftEntity selectLift(String liftId) {
//        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
//        liftEntityQueryWrapper.eq("lift_id", liftId);
//        LiftEntity selectLiftEntity = liftMapper.selectOne(liftEntityQueryWrapper);
//        return selectLiftEntity;
//    }
//
//    private void updateLiftTaskState(Integer id,Integer state) {
//        LiftAgvcTaskEntity liftAgvTaskEntity = new LiftAgvcTaskEntity();
//        liftAgvTaskEntity.setState(state);
//        QueryWrapper<LiftAgvcTaskEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("id", id);
//        liftAgvcTaskMapper.update(liftAgvTaskEntity,queryWrapper);
//    }
//}
